﻿using KBWebApi.Common;
using KBWebApi.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace KBWebApi.Dal
{
    //Test_测试传输修改
    public class UserDal
    {
        private static SqlSugarScope db = SqlDBHelper.SqlHelper.GetDB();

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="dcdUserID"></param>
        /// <param name="psd"></param>
        /// <returns></returns>
        public static UserDto GetUser(string dcdUserID, string psd)
        {
            UserDto userReturn = new UserDto();
            userReturn = db.Queryable<UserDto>().First(t => t.UserId == dcdUserID && t.Password == psd);
            return userReturn;
        }

        internal static UserDto GetUserInfo(string userId)
        {
            UserDto userReturn = new UserDto();
            userReturn = db.Queryable<UserDto>().First(t => t.UserId == userId);
            return userReturn;
        }

        public static bool IsExists(UserDto user)
        {
            try
            {
                bool isexists = db.Queryable<UserDto>().Any(t => t.UserId == user.UserId);
                return isexists;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("User.IsExists=>" + ex.ToString());
                return false;
            }
        }

        public int NewKBInput(UserDto user)
        {
            int count = db.Insertable(user).IgnoreColumns(t => new { t.UpdateDate, t.Updater }).ExecuteCommand();
            return count;
        }

        public int Update(UserDto user)
        {
            int count = db.Updateable(user).IgnoreColumns(t => new { t.CreateDate, t.Creator }).ExecuteCommand();
            return count;
        }

        public int Delete(string UserId)
        {
            int count = db.Deleteable<UserDto>().Where(t => t.UserId == UserId).ExecuteCommand();
            return count;
        }

        public static DataTable GetList(string UserId, string UserName)
        {
            var exp = Expressionable.Create<UserDto>()
                .OrIF(!string.IsNullOrEmpty(UserId), t => t.UserId == UserId)
                .OrIF(!string.IsNullOrEmpty(UserName), t => t.UserName == UserName).ToExpression();
            return db.Queryable<UserDto, WorkShopModel>((t, p) => new object[] { JoinType.Left, t.WorkShopId == p.XS_WorkShopId })
                .Where(exp)
                .With(SqlWith.NoLock)
                .OrderBy(t => new { t.UserId })
                .Select((t, p) => new { t.UserId, t.UserName, t.Password, t.Role, t.WorkShopId, p.WorkShop, t.CreateDate, t.Creator, t.UpdateDate, t.Updater })
                .ToDataTable();
        }

        public int UpdateUserInfo(string userId, string userName, string oldPwd, string newPwd)
        {
            int count = db.Updateable<UserDto>()
                .SetColumnsIF(!string.IsNullOrEmpty(userName),t =>  t.UserName == userName)
                .SetColumns(t => t.Password == newPwd)
                .SetColumns(t => t.UpdateDate == DateTime.Now)
                .SetColumns(t => t.Updater == userId)
                .Where(t =>  t.UserId == userId && t.Password == oldPwd)
                .ExecuteCommand();
            return count;
        }
    }
}
